﻿using AgencyManagerSystem.AsmType;
using AgencyManagerSystem.Common;
using AgencyManagerSystem.Core;
using AgencyManagerSystem.DataFetcher;
using AgencyManagerSystem.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace AgencyManagerSystem.Controllers
{
    public class CustomerController : MyControllerBase
    {
        CustomerManager manager;
        CustomerFetcher fetcher;
        public CustomerController() : base()
        {
            this.manager = new CustomerManager();
            this.fetcher = new CustomerFetcher();
        }
        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Retail, AppOpt.Create)]
        public AsmObject<CustomerModel> Set(CustomerModel customerModel)
        {
            if (customerModel == null)
            {
                DBHelper.Commit( false);
                LogHelper.Err(this.GetType(), String.Format("SET:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return new AsmObject<CustomerModel>() { Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
            }
            LogHelper.Debug(this.GetType(), String.Format("SET:[RQS|{1}]{0}", Util.ConvertToJson(customerModel), RequestIp));
            var result = manager.Set(customerModel);
            LogHelper.Debug(this.GetType(), String.Format("SET:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            DBHelper.Commit( true);
            return result;
        }
        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Center, AppOpt.Delete)]
        public AsmObject<bool> Del(CustomerModel customerModel)
        {
            if (customerModel == null)
            {
                DBHelper.Commit( false);
                LogHelper.Err(this.GetType(), String.Format("SET:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return new AsmObject<bool>() { Data = false, Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
            }
            LogHelper.Debug(this.GetType(), String.Format("DEL:[RQS|{1}]{0}", Util.ConvertToJson(customerModel), RequestIp));
            var result = manager.Delete(customerModel);
            LogHelper.Debug(this.GetType(), String.Format("DEL:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            DBHelper.Commit( true);
            return result;
        }
        [HttpGet]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Retail, AppOpt.Exec)]
        public AsmArray<CustomerModel> List(string jsonCondition, int pos, int size)
        {
            LogHelper.Debug(this.GetType().Name, String.Format("LIST:[RQS|{1}]{0}", jsonCondition, RequestIp));
            int cnt = 0;
            var customerModels = fetcher.List(Util.ConvertArrayFromJson<Condition>(jsonCondition), pos, size, out cnt);
            DBHelper.Commit( true);
            var result = new AsmArray<CustomerModel>() { Data = customerModels, Message = "操作成功", Result = AmsCode.Succ, Cnt = cnt };
            LogHelper.Debug(this.GetType().Name, String.Format("LIST:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpGet]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Retail, AppOpt.Exec)]
        public AsmObject<CustomerModel> Get(int id)
        {
            LogHelper.Debug(this.GetType().Name, String.Format("GET:[RQS|{1}]{0}", id, RequestIp));
            var customer = fetcher.Get(id);
            DBHelper.Commit(true);
            AsmObject<CustomerModel> result = null;
            if (customer != null)
                result = new AsmObject<CustomerModel>() { Data = customer, Message = "操作成功", Result = AmsCode.Succ };
            else
                result = new AsmObject<CustomerModel>() { Data = customer, Message = "无法找到数据", Result = AmsCode.DataNotFound };
            LogHelper.Debug(this.GetType(), String.Format("GET:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpGet]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Retail, AppOpt.Exec)]
        public AsmArray<CustomerModel> ListByRetail(int retail,string customername)
        {
            if (customername == null)
                customername = "%";
            customername = "%" + customername + "%";
            LogHelper.Debug(this.GetType(), String.Format("LISTBYRETAIL:[RQS|{1}]{0}", retail, RequestIp));
            var list = fetcher.ListByRetail(retail,customername);
            DBHelper.Commit(true);
            var result = new AsmArray<CustomerModel>() { Data = list, Message = "操作成功", Result = AmsCode.Succ };
            LogHelper.Debug(this.GetType(), String.Format("LISTBYRETAIL:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
		[HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Center, AppOpt.Delete)]
        public AsmArray<Int32> BulkDel(dynamic jsonCustomerModel)
        {
            LogHelper.Debug(this.GetType(), String.Format("BULKDEL:[RQS|{1}]{0}", jsonCustomerModel, RequestIp));
            var models = Util.ConvertArrayFromJson<CustomerModel>((String)jsonCustomerModel.data);
            if (models == null)
            {
                LogHelper.Err(this.GetType(), String.Format("BULKDEL:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (Int32)HttpStatusCode.BadRequest;
                var errResponse = new AsmArray<Int32>() { Data = new List<Int32>(), Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
                LogHelper.Debug(this.GetType(), String.Format("BULKDEL:[RSP|{1}]{0}", Util.ConvertToJson(errResponse), RequestIp));
                return errResponse;
            }
            int faild = models.Count();
            var del = new List<Int32>();
            StringBuilder sb = new StringBuilder();
            foreach (var model in models)
            {
                var eff = manager.Delete(model);
                if (eff.Result != 0)
                {
                    sb.AppendFormat("{0}:{1}\r\n", model.AutoId, eff.Message);
                }
                else
                {
                    del.Add(model.AutoId);
                    faild--;
                }
            }
            DBHelper.Commit(true);
            var succResponse = new AsmArray<Int32>() { Data = del, Message = "共有" + (models.Count() - faild) + "笔资料删除成功", Result = AmsCode.Succ };
            LogHelper.Debug(this.GetType(), String.Format("BULKDEL:[RSP|{1}]{0}", Util.ConvertToJson(succResponse), RequestIp));
            return succResponse;
        }
    }
}